package com.guinea.account.service;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import net.sf.navigator.menu.MenuComponent;
import net.sf.navigator.menu.MenuRepository;

import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.util.Assert;

import com.guinea.account.dao.MenuDao;
import com.guinea.account.entity.Menu;
import com.guinea.account.entity.Role;
import com.guinea.core.Constants;
import com.guinea.core.orm.hibernate.HibernateDao;
import com.guinea.core.service.CRUDManager;

/**
 * @author huaxiaz
 */
public class MenuManager extends CRUDManager {
	private static final String QUERY_CHILDREN_BY_PARENTID = "select m from com.guinea.account.entity.Menu m where m.parentId=? order by m.serial asc";

	private MenuDao menuDao;

	public HibernateDao getEntityDao() {
		return getMenuDao();
	}

	public Menu getMenu(Serializable id) {
		return (Menu)getMenuDao().get(id);
	}

	public MenuDao getMenuDao() {
		return menuDao;
	}

	public void setMenuDao(MenuDao menuDao) {
		this.menuDao = menuDao;
	}

	public MenuRepository getRepository(){
		List menus = getValidSerial();
		MenuRepository repository = new MenuRepository();
		for (Iterator iter = menus.iterator(); iter.hasNext();) {
			Menu menu = (Menu) iter.next();
			if(menu == null || menu.getId() == null) continue; 
			Menu parent = null;
			if(menu.getParentId() != null)
				parent = (Menu)getEntityDao().get(menu.getParentId());
			MenuComponent parentMenu = null;
			if(parent != null && parent.getId() != null)
				parentMenu = repository.getMenu(parent.getId().toString());
			if(parentMenu == null && parent != null && parent.getId().longValue() != 0){
				parentMenu = createMenuComponent(parent);
				repository.addMenu(parentMenu);
			}
			MenuComponent mc = createMenuComponent(menu);
			if(menu.getParentId().longValue() != 0L)
				mc.setParent(parentMenu);
			repository.addMenu(mc);
		}
		
		return repository;
	}
	private MenuComponent createMenuComponent(Menu menu){
		MenuComponent mc = new MenuComponent();
		mc.setName(menu.getMenuName());
		mc.setTitle(menu.getTitle());
		String type = menu.getMenuType();
		if("action".equalsIgnoreCase(type)){
			mc.setAction(menu.getMenuUrl());
		}
		if("".equalsIgnoreCase(type)){
			mc.setForward(menu.getMenuUrl());
		}
		if("".equalsIgnoreCase(type)){
			mc.setLocation(menu.getMenuUrl());
		}
		if("page".equalsIgnoreCase(type)){
			mc.setPage(menu.getMenuUrl());
		}
		if("".equalsIgnoreCase(type)){
			mc.setTarget(menu.getMenuUrl());
		}
		if("".equalsIgnoreCase(type)){
			mc.setUrl(menu.getMenuUrl());
		}
		Set roles = menu.getRoles();
		String  roleString = "";
		for (Iterator iter = roles.iterator(); iter.hasNext();) {
			Role role = (Role) iter.next();
			roleString += role.getId().longValue();
			if(iter.hasNext()) roleString += ",";
		}
		if(!roleString.trim().equals(""))
			mc.setRoles(roleString);
		return mc;
	}
	/**
	 * 获取全部标记可用对象,顺序排列
	 */
	public List getValidSerial(){
		Criterion criterion= Restrictions.eq(Constants.STATUS_PROPERTY, Constants.STATUS_VALID);
		Criteria c = getEntityDao().createCriteria(criterion);
		c.addOrder(Order.asc("parentId"));
		c.addOrder(Order.asc("serial"));
		return c.list();
	}
	
	public String[] getChildrenIds(Serializable id){
		List children = getEntityDao().createQuery(QUERY_CHILDREN_BY_PARENTID,id).list();
		String[] childrenIds = new String[children.size()];
		int i = 0;
		for (Iterator iter = children.iterator(); iter.hasNext();i++) {
			Menu child = (Menu) iter.next();
			childrenIds[i] = child.getId().toString();
		}
		return childrenIds;
	}
    public String[] getRelativeRoleIds(Serializable id) {
        Role role;
        Menu menu = (Menu)get(id);
        Set roles = menu.getRoles();
        String[] relativeRoleIds = new String[roles.size()];
        int i = 0;
        for (java.util.Iterator iter = roles.iterator(); iter.hasNext();) {
            role = (Role) iter.next();
            Assert.notNull(role.getId());
            relativeRoleIds[i] = String.valueOf(role.getId().longValue());
            i++;
        }
        return relativeRoleIds;
    }
}
